import 'package:creatures_world/data/animal_data_tool.dart';
import 'package:creatures_world/framework/custom/ext_widget/widget_util.dart';
import 'package:creatures_world/framework/utils/sized_box_util.dart';
import 'package:creatures_world/net/taxon_service.dart';
import 'package:creatures_world/pages/base/base_page.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:getwidget/components/toast/gf_toast.dart';
import 'package:google_fonts/google_fonts.dart';
import '../../get/routers.dart';
import 'func_controller.dart';

class FuncPage extends BasePage<FuncController> {



  @override
  Widget build(BuildContext context) {
    controller.awake();
    return Scaffold(
      backgroundColor: Colors.blueAccent,
     /* appBar: PreferredSize(
        preferredSize: Size.fromHeight(60.h),
        child: SafeArea(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 10.w),
            child: Row(
              children: [
                Icon(Icons.arrow_back_ios_sharp, color: Colors.white, size: 25.h).prop(
                  onTap: () {
                    Get.back();
                  },
                ),
                SizedBox(width: 10.w),
                Expanded(
                  child: Container(
                    height: 35.h,
                    decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8)),
                    child: TextField(
                      controller: controller.searchTextEditingController,
                      style: GoogleFonts.poppins(fontSize: 12.sp, fontWeight: FontWeight.w300),
                      decoration: InputDecoration(
                        hintText: 'Search species name...',
                        suffixIcon: Icon(Icons.search).prop(
                          onTap: () async {
                            debugPrint(" click search start");
                            var matchedList = await compute(
                              FuncController.onSearchInput,
                              SearchParams(controller.searchTextEditingController.text, AnimalDataTool.animalMap),
                            );
                            controller.matchedList.value = matchedList;
                          },
                        ),
                        border: InputBorder.none,
                        contentPadding: EdgeInsets.symmetric(horizontal: 5, vertical: 5),
                      ),
                      onSubmitted: (value) {
                        print('搜索内容: $value');
                      },
                    ),
                  ),
                ),
                SizedBox(width: 10.w),

              ],
            ),
          ),
        ),
      ),*/
      body: SafeArea(
        child: Column(
          children: [

            Container(
              width: double.infinity,
              alignment: Alignment.center,
              padding: EdgeInsets.only(top: 5.h,bottom: 5.h),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  sb.w(15.w),
                  Icon(Icons.arrow_back_ios_sharp, color: Colors.white, size: 20.h).prop(
                    onTap: () {
                      Get.back();
                    },
                  ),
                  SizedBox(width: 10.w),
                  Expanded(
                    child: Container(
                      height: 30.h,
                      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8)),
                      child: TextField(
                        controller: controller.searchTextEditingController,
                        style: GoogleFonts.poppins(fontSize: 12.sp, fontWeight: FontWeight.w300),
                        decoration: InputDecoration(
                          hintText: 'Search species name...',
                          suffixIcon: Icon(Icons.search).prop(
                            onTap: () async {
                              debugPrint(" click search start");
                              var matchedList = await compute(
                                FuncController.onSearchInput,
                                SearchParams(controller.searchTextEditingController.text, AnimalDataTool.animalMap),
                              );
                              controller.matchedList.value = matchedList;
                            },
                          ),
                          border: InputBorder.none,
                          contentPadding: EdgeInsets.symmetric(horizontal: 5, vertical: 0),
                        ),
                        onSubmitted: (value) {
                          print('搜索内容: $value');
                        },
                      ),
                    ),
                  ),
                  sb.w(15.w),
                ],
              ),
            ),
            Divider(height: 1,),
            Expanded(
              child: Container(
                margin: EdgeInsets.only(left: 15.w, right: 15.w),
                child: Obx(() {
                  int itemCount = controller.matchedList.value.length;
                  return ListView.separated(
                    padding: EdgeInsets.zero,
                    itemBuilder: (ctx, index) {
                      dynamic data = controller.matchedList.value[index];
                      return Container(
                        width: double.infinity,
                        height: 30.h,
                        child: Row(
                          children: [
        
        
                            Text("${data["name"]}", style: GoogleFonts.poppins(fontWeight: FontWeight.w400, color: Colors.white, fontSize: 14.sp)).prop(
                              onTap: () {
                                debugPrint("--->>   ${data}");
        
                                TaxonService.getSpeciesDetail(data["id"]).then((taxonResponse){
                                  if(taxonResponse.isOk() && taxonResponse.data_list.isNotEmpty){
                                    Get.toNamed(
                                      AppPage.ROUTER_SPDETAIL,
                                      arguments: {"detail": taxonResponse.data_list[0]},
                                    );
                                  }else{
                                    GFToast.showToast("No data", context);
                                  }
                                });
        
                              },
                            ),
                          ],
                        ),
                      );
                    },
                    separatorBuilder: (ctx, index) {
                      return sb.h(1.h);
                    },
                    itemCount: itemCount,
                  );
                }),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
